﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FIFP.Common;
using FIFP.DashBoard.Helper;
using FIFP.DashBoard.Views;
using FIFP.DashBoard.Model;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using FIFP.DashBoard.Converters;
using System.Threading;
using System.Windows.Browser;
using FIFP.DashBoard.RelationalDataService;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace FIFP.DashBoard.ViewModel
{
    public class MainPageVM : ViewModelCore
    {
        #region Variables
        public event EventHandler OnLinkedShutdownClick;
        public event EventHandler OnOverlapClick;
        public event EventHandler OnImportClick;
        public event EventHandler OnOptimizationClick;
        public event EventHandler OnScenarioClick;

        #endregion

        #region Properties

        private string scenarioName = string.Empty;
        public string ScenarioName
        {
            get { return scenarioName; }
            set { SetPropertyValue(() => ScenarioName, value, val => scenarioName = val); }
        }

        private bool isLoading = false;
        public bool IsLoading
        {
            get { return isLoading; }
            set { SetPropertyValue(() => IsLoading, value, val => isLoading = val); }
        }

        private string loadingMessage;
        public string LoadingMessage
        {
            get { return loadingMessage; }
            set { SetPropertyValue(() => LoadingMessage, value, val => loadingMessage = val); }
        }

        private TreeView lsdTreeView;
        public TreeView LsdTreeView
        {
            get { return lsdTreeView; }
            set { SetPropertyValue(() => LsdTreeView, value, val => lsdTreeView = val); }
        }

        private Grid mainGrid;
        public Grid MainGrid
        {
            get { return mainGrid; }
            set { SetPropertyValue(() => MainGrid, value, val => mainGrid = val); }
        }

        private bool _IsImportChecked;
        public bool IsImportChecked
        {
            get { return _IsImportChecked; }
            set { SetPropertyValue(() => IsImportChecked, value, val => _IsImportChecked = val); }
        }

        private bool _IsOptimizationChecked;
        public bool IsOptimizationChecked
        {
            get { return _IsOptimizationChecked; }
            set { SetPropertyValue(() => IsOptimizationChecked, value, val => _IsOptimizationChecked = val); }
        }

        private bool _IsScenarioChecked;
        public bool IsScenarioChecked
        {
            get { return _IsScenarioChecked; }
            set { SetPropertyValue(() => IsScenarioChecked, value, val => _IsScenarioChecked = val); }
        }

        private Visibility? _IsSubMenuVisible = Visibility.Collapsed;
        public Visibility? IsSubMenuVisible
        {
            get { return _IsSubMenuVisible; }
            set { SetPropertyValue(() => IsSubMenuVisible, value, val => _IsSubMenuVisible = val); }
        }


        private bool _IsOverLapChecked = true;
        public bool IsOverLapChecked
        {
            get { return _IsOverLapChecked; }
            set { SetPropertyValue(() => IsOverLapChecked, value, val => _IsOverLapChecked = val); }
        }

        private bool _IsLinkedSDChecked;
        public bool IsLinkedSDChecked
        {
            get { return _IsLinkedSDChecked; }
            set { SetPropertyValue(() => IsLinkedSDChecked, value, val => _IsLinkedSDChecked = val); }
        }

        private bool _IsOptMngChecked;
        public bool IsOptMngChecked
        {
            get { return _IsOptMngChecked; }
            set { SetPropertyValue(() => IsOptMngChecked, value, val => _IsOptMngChecked = val); }
        }

        private Style _SaveAsStyle = Application.Current.Resources[FIFPConstants.BUTTON_DEFAULTCOLOR] as Style;
        public Style SaveAsStyle
        {
            get { return _SaveAsStyle; }
            set { SetPropertyValue(() => SaveAsStyle, value, val => _SaveAsStyle = val); }
        }

        private Style _SaveStyle = Application.Current.Resources[FIFPConstants.BUTTON_DEFAULTCOLOR] as Style;
        public Style SaveStyle
        {
            get { return _SaveStyle; }
            set { SetPropertyValue(() => SaveStyle, value, val => _SaveStyle = val); }
        }
        private bool _IsIntegrationEnabled = false;
        public bool IsIntegrationEnabled
        {
            get { return _IsIntegrationEnabled; }
            set { SetPropertyValue(() => IsIntegrationEnabled, value, val => _IsIntegrationEnabled = val); }
        }

        private bool _IsScenarioCompareEnabled = false;
        public bool IsScenarioCompareEnabled
        {
            get { return _IsScenarioCompareEnabled; }
            set { SetPropertyValue(() => IsScenarioCompareEnabled, value, val => _IsScenarioCompareEnabled = val); }
        }

        private bool isLSDEnabled = true;
        public bool IsLSDEnabled
        {
            get { return isLSDEnabled; }
            set { SetPropertyValue(() => IsLSDEnabled, value, val => isLSDEnabled = val); }
        }

        private bool isSaveButtonEnabled = true;
        public bool IsSaveButtonEnabled
        {
            get { return isSaveButtonEnabled; }
            set { SetPropertyValue(() => IsSaveButtonEnabled, value, val => isSaveButtonEnabled = val); }
        }

        private bool isUnodButtonEnabled = true;
        public bool IsUnodButtonEnabled
        {
            get { return isUnodButtonEnabled; }
            set { SetPropertyValue(() => IsUnodButtonEnabled, value, val => isUnodButtonEnabled = val); }
        }

        private bool isCalculateButtonEnabled = true;
        public bool IsCalculateButtonEnabled
        {
            get { return isCalculateButtonEnabled; }
            set { SetPropertyValue(() => IsCalculateButtonEnabled, value, val => isCalculateButtonEnabled = val); }
        }

        private Visibility? _UserExistVisibility = Visibility.Collapsed;
        public Visibility? UserExistVisibility
        {
            get { return _UserExistVisibility; }
            set { SetPropertyValue(() => UserExistVisibility, value, val => _UserExistVisibility = val); }
        }

        private Visibility? _UserNotExistVisibility = Visibility.Collapsed;
        public Visibility? UserNotExistVisibility
        {
            get { return _UserNotExistVisibility; }
            set { SetPropertyValue(() => UserNotExistVisibility, value, val => _UserNotExistVisibility = val); }
        }

        private FIFPGroupUser loginUserRole;
        public FIFPGroupUser LoginUserRole
        {
            get { return loginUserRole; }
            set { SetPropertyValue(() => LoginUserRole, value, val => loginUserRole = val); }
        }
        #endregion

        #region Commands

        private ICommand _NavigationCommand;
        public ICommand NavigationCommand
        {
            get
            {
                return _NavigationCommand ??
                        (_NavigationCommand = new RelayCommand<RadioButton>(Navigation));
            }
        }

        private ICommand _PageLoadCommand;
        public ICommand PageLoadCommand
        {
            get
            {
                return _PageLoadCommand ??
                        (_PageLoadCommand = new RelayCommand<Grid>(InitializePage));
            }
        }

        private ICommand _SaveCommand;
        public ICommand SaveCommand
        {
            get
            {
                return _SaveCommand ??
                        (_SaveCommand = new RelayCommand<Button>(DoSaveButtonLogic));
            }
        }

        private ICommand _USSCFieldClickCommand;
        public ICommand USSCFieldClickCommand
        {
            get
            {
                return _USSCFieldClickCommand ??
                        (_USSCFieldClickCommand = new RelayCommand<Image>(USSCFieldClick));
            }
        }

        private ICommand _UndoCommand;
        public ICommand UndoCommand
        {
            get
            {
                return _UndoCommand ??
                        (_UndoCommand = new RelayCommand<Button>(UndoActivities));
            }
        }

        private ICommand _UserGuideCommand;
        public ICommand UserGuideCommand
        {
            get
            {
                return _UserGuideCommand ??
                        (_UserGuideCommand = new RelayCommand<HyperlinkButton>(LoadUserManual));
            }
        }

        #endregion

        #region Constructor
        public MainPageVM()
        {
            IsLoading = true;
            if (App.UserGroupId == string.Empty)
            {
                UserExistVisibility = Visibility.Collapsed;
                UserNotExistVisibility = Visibility.Visible;
            }
            else
            {
                UserExistVisibility = Visibility.Visible;
                UserNotExistVisibility = Visibility.Collapsed;
            }
        }
        #endregion

        #region Methods
       

        #region InitializePage
        private void InitializePage(Grid layoutRoot)
        {
            if (MainGrid == null)
            {
                MainGrid = layoutRoot;
                IsImportChecked = true;
                if (MainGrid != null && MainGrid.Children != null && MainGrid.Children.Count > 0)
                    MainGrid.Children.Clear();
                MainGrid.Children.Add(ServiceLocator.Get<Import>());
            }
        }
        #endregion

        #region Undo logic
        private void UndoActivities(Button btnUndo)
        {
            ServiceLocator.Get<ImportVM>().UndoScheduleViewActivities();
        }
        #endregion

        #region Load user manual
        private void LoadUserManual(HyperlinkButton sender)
        {
            string pdfFileName = string.Empty;
            if (IsImportChecked)
            {
                if (ServiceLocator.Get<ImportVM>().IsCalButtonEnabled)
                    pdfFileName = App.HelpFilePath + FIFPConstants.HELPFILE_HOME_PAGE;
                else
                    pdfFileName = App.HelpFilePath + FIFPConstants.HELPFILE_HOME_INITIAL_PAGE;
            }
            else if (IsOptimizationChecked && IsLinkedSDChecked)
            {
                pdfFileName = App.HelpFilePath + FIFPConstants.HELPFILE_LSD_PAGE;
            }
            else if (IsOptimizationChecked && IsOptMngChecked)
            {
                pdfFileName = App.HelpFilePath + FIFPConstants.HELPFILE_OPTI_PAGE;
            }
            else if (IsOptimizationChecked && IsOverLapChecked)
            {
                pdfFileName = App.HelpFilePath + FIFPConstants.HELPFILE_OVERLAP_PAGE;
            }
            else if (IsScenarioChecked)
            {
                pdfFileName = App.HelpFilePath + FIFPConstants.HELPFILE_SCENARIO_PAGE;
            }

            if (!string.IsNullOrEmpty(pdfFileName))
            {
                string url = HtmlPage.Document.DocumentUri.AbsoluteUri;
                url = url.Substring(0, url.IndexOf(HtmlPage.Document.DocumentUri.LocalPath));

                Uri b = new Uri(String.Format("{0}{1}", url.ToString(), pdfFileName));
                HtmlPage.Window.Navigate(b, "_blank");
            }
        }
        #endregion

        #region Navigation
        private void Navigation(RadioButton rdBtn)
        {

            if (ServiceLocator.Get<ImportVM>().IsAnimationStarted)
            {
                ServiceLocator.Get<ImportVM>().IsAnimationStarted = false;
                StoryboardManager.PlayStoryboard(FIFPConstants.SETTINGSFLIP_STOP, s => { }, null);
            }
            //ServiceLocator.Get<AppointmentVM>().MinTimeRulerExtentValue = 0;
            IsImportChecked = IsOptimizationChecked = IsScenarioChecked = false;
            //ServiceLocator.Get<FilterVM>().ResetFilterData();
            if (MainGrid != null && MainGrid.Children != null && MainGrid.Children.Count > 0)
                MainGrid.Children.Clear();
            if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_IMPORT)
            {
                MainGrid.Children.Add(ServiceLocator.Get<Import>());
                IsImportChecked = true;
                IsOptimizationChecked = IsScenarioChecked = false;
                IsSubMenuVisible = Visibility.Collapsed;
                ServiceLocator.Get<FilterVM>().IsDateTimeVisibility = Visibility.Visible;
                ServiceLocator.Get<FilterVM>().IsCombinedTowerVisibility = Visibility.Collapsed;
                if (OnImportClick != null)
                    OnImportClick(this, null);
            }
            else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_OPTIMIZATION)
            {
                IsOptimizationChecked = true;
                IsImportChecked = IsScenarioChecked = false;
                IsSubMenuVisible = Visibility.Visible;

                if (IsOverLapChecked)
                {
                    MainGrid.Children.Add(ServiceLocator.Get<OverlapManagement_New>());
                    if (OnOverlapClick != null)
                        OnOverlapClick(this, null);
                }
                else if (IsLinkedSDChecked)
                {
                    MainGrid.Children.Add(ServiceLocator.Get<LSDManagement>());
                    ServiceLocator.Get<FilterVM>().IsDateTimeVisibility = Visibility.Collapsed;
                    ServiceLocator.Get<FilterVM>().IsCombinedTowerVisibility = Visibility.Visible;
                    if (OnLinkedShutdownClick != null)
                        OnLinkedShutdownClick(this, null);
                }
                else
                {
                    MainGrid.Children.Add(ServiceLocator.Get<OptManagement_New>());
                    ServiceLocator.Get<FilterVM>().IsDateTimeVisibility = Visibility.Collapsed;
                    ServiceLocator.Get<FilterVM>().IsCombinedTowerVisibility = Visibility.Collapsed;
                    if (OnOptimizationClick != null)
                        OnOptimizationClick(this, null);
                }
            }
            else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_OVERLAP)
            {
                MainGrid.Children.Add(ServiceLocator.Get<OverlapManagement_New>());
                IsOptimizationChecked = true;
                IsImportChecked = IsScenarioChecked = false;
                IsSubMenuVisible = Visibility.Visible;
                IsOverLapChecked = true;
                IsLinkedSDChecked = IsOptMngChecked = false;
                if (OnOverlapClick != null)
                    OnOverlapClick(this, null);
            }
            else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_OPTMNG)
            {
                MainGrid.Children.Add(ServiceLocator.Get<OptManagement_New>());
                IsOptimizationChecked = true;
                IsImportChecked = IsScenarioChecked = false;
                IsSubMenuVisible = Visibility.Visible;
                IsOptMngChecked = true;
                IsLinkedSDChecked = IsOverLapChecked = false;
                ServiceLocator.Get<FilterVM>().IsDateTimeVisibility = Visibility.Collapsed;
                ServiceLocator.Get<FilterVM>().IsCombinedTowerVisibility = Visibility.Collapsed;
                if (OnOptimizationClick != null)
                    OnOptimizationClick(this, null);
            }
            else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_SHUTDOWN)
            {
                MainGrid.Children.Add(ServiceLocator.Get<LSDManagement>());
                IsOptimizationChecked = true;
                IsImportChecked = IsScenarioChecked = false;
                IsSubMenuVisible = Visibility.Visible;
                IsLinkedSDChecked = true;
                IsOptMngChecked = IsOverLapChecked = false;
                ServiceLocator.Get<FilterVM>().IsDateTimeVisibility = Visibility.Collapsed;
                ServiceLocator.Get<FilterVM>().IsCombinedTowerVisibility = Visibility.Visible;
                if (OnLinkedShutdownClick != null)
                    OnLinkedShutdownClick(this, null);
            }
            else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_SCENARIO)
            {
                MainGrid.Children.Add(ServiceLocator.Get<Scanario>());
                IsScenarioChecked = true;
                IsImportChecked = IsOptimizationChecked = false;
                IsSubMenuVisible = Visibility.Collapsed;
                if (OnScenarioClick != null)
                    OnScenarioClick(this, null);
            }
        }
        #endregion

        private void DoSaveButtonLogic(Button btnSave)
        {
            IsSaveButtonEnabled = false;
            IsCalculateButtonEnabled = false;
            string tag = string.Empty;
            if (btnSave != null && btnSave.Tag != null)
                tag = Convert.ToString(btnSave.Tag);
            switch (tag)
            {

                case FIFPConstants.HYPERLINK_CALCULATE:
                    SaveAsStyle = Application.Current.Resources[FIFPConstants.BUTTON_SELECTEDCOLOR] as Style;
                    SaveStyle = Application.Current.Resources[FIFPConstants.BUTTON_DEFAULTCOLOR] as Style;
                    ServiceLocator.Get<ImportVM>().Calculate(btnSave);
                    break;
                case FIFPConstants.HYPERLINK_SAVE:
                    SaveStyle = Application.Current.Resources[FIFPConstants.BUTTON_SELECTEDCOLOR] as Style;
                    SaveAsStyle = Application.Current.Resources[FIFPConstants.BUTTON_DEFAULTCOLOR] as Style;
                    ServiceLocator.Get<ImportVM>().SaveScenario();
                    break;
            }
        }

        private void USSCFieldClick(Image usscImage)
        {
            string queryString = "navType=import";

            string url = HtmlPage.Document.DocumentUri.AbsoluteUri;
            url = url.Substring(0, url.IndexOf(HtmlPage.Document.DocumentUri.LocalPath));
            string pageName = App.CustomMainPageName;

            Uri b = new Uri(String.Format("{0}{1}", url.ToString(), "/" + pageName + (pageName.Contains("?") ? "&" : "?") + queryString));
            HtmlPage.Window.Navigate(b, "_parent");
        }

        #endregion
    }
}
